package com.ytkj.ygAssist.viewControl;

import com.ytkj.ygAssist.main.YunGouLoginServer;
import com.ytkj.ygAssist.main.YunGouServer;
import com.ytkj.ygAssist.tools.Config;
import com.ytkj.ygAssist.tools.JFrameListeningInterface;
import com.ytkj.ygAssist.view.Foreknow;
import com.ytkj.ygAssist.view.myView.ShowMyMenu;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class ForeknowControl extends Foreknow {
	private static final long serialVersionUID = 1L;

	public ForeknowControl() {
		super();
		addButtonMouseListener();
		setListening();
	}

	/*
	 * 添加按钮监听事件
	 */
	private void addButtonMouseListener() {
		selectButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (selectButton.getText().equals("查  询")) {
					if (Config.isAssistOverdue) {
						ShowMyMenu.ShowAssistOverdue();
						return;
					}
					selectButton();
				} else {
					setButtonEnabled(true);
					selectButton.setText("查  询");
					selectInfoLabel.setText("停止查询");
					if (thread != null) {
						thread.stop();
						thread = null;
					}
				}
			}
		});
	}

	private void setListening() {
		foreknowInterface = new JFrameListeningInterface() {
			@Override
			public void setFrameListeningText(String FrameName, String[] text) {
				if (selectButton.getText().equals("停止查询") && text[0].equals(goodsIdTextField.getText())) {
					DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
					int index = 0;
					if ("0".equals(FrameName)) {
						tableModel.setValueAt(text[0], index, 0);
						tableModel.setValueAt(text[1], index, 1);
					} else {
						for (int i = 0; i < tableModel.getRowCount(); i++) {
							if (tableModel.getValueAt(i, 1).toString().equals(text[1])) {
								index = i;
								break;
							}
						}
					}
					if (!"搜索超时".equals(text[2])) {
						tableModel.setValueAt(text[2], index, 2);
						tableModel.setValueAt(text[4], index, 4);
						if (!text[2].equals("") || text[2].equals("正在云购中")) {
							tableModel.setValueAt(text[3], index, 3);
							if (!text[2].equals("正在云购中")) {
								if (verifyWinCheckBox.isSelected()
										&& tableModel.getValueAt(index, 7).toString().equals("")) {
									if (YunGouLoginServer.getUserServer().getIsLogin()) {// 验证用户中奖
										verificationUserWinning(tableModel, text, index);
									} else {
										verifyWinCheckBox.setSelected(false);
										tableModel.setColumnCount(7);
									}
								}
							}
						}
					}
					if (!"".equals(text[5]) && tableModel.getValueAt(index, 5).toString().equals("")) {
						boolean isSelectOver = false;
						if (latelyPeriodComboBox.getSelectedIndex() == 1) {
							isSelectOver = true;
						} else {
							selectNum++;
							if (selectNum >= selectNumComboBox.getSelectedIndex() + 2) {
								isSelectOver = true;
							}
						}
						if (isSelectOver) {
							selectInfoLabel.setText((System.currentTimeMillis() - beginTime) / 1000f + " 秒 ");
							selectButton.setText("查  询");
							setButtonEnabled(true);
						}
					}
					tableModel.setValueAt(text[5], index, 5);
					tableModel.setValueAt(text[6], index, 6);
				}
			}

			@Override
			public void setFrameText(String FrameName, String text) {
				if (FrameName.equals("setGoodsName")) {
					goodsNameLblid.setText(text);
				} else if (FrameName.equals("webError")) {
					String[] text2 = new String[] { text.split("___")[0], text.split("___")[1], "搜索超时", "网络繁忙", "网络繁忙",
							"网络繁忙", "网络繁忙" };
					foreknowInterface.setFrameListeningText(null, text2);
				} else if (FrameName.equals("isSelect")) {
					String[] text2 = new String[] { text.split("___")[0], text.split("___")[1], "搜索超时", "正在查询", "正在查询",
							"正在查询", "正在查询" };
					foreknowInterface.setFrameListeningText(null, text2);
				} else if (FrameName.equals("selectError")) {
					String[] text2 = new String[] { text.split("___")[0], text.split("___")[1], "搜索异常", "请检查输入商品ID",
							"或商品期数", "或检查网络", "搜索异常" };
					foreknowInterface.setFrameListeningText(null, text2);
				} else if (FrameName.equals("setGoodsNameError")) {
					selectButton.setText("查  询");
					setButtonEnabled(true);
					selectInfoLabel.setText("未查到此商品");
				} else if (FrameName.equals("goodsoldOut")) {
					selectButton.setText("查  询");
					setButtonEnabled(true);
					selectInfoLabel.setText("该商品已经下降了");
				} else if (FrameName.equals("initTable")) {
					String goodsId = goodsIdTextField.getText().trim();
					int period = Integer.valueOf(text);
					TableModel tableModel = table.getModel();
					for (int i = 0; i < selectNumComboBox.getSelectedIndex() + 2 && period > 0; i++) {
						tableModel.setValueAt(goodsId, i, 0);
						tableModel.setValueAt(period--, i, 1);
					}
				}
			}
		};
	}

	private void selectButton() {
		if (goodsIdTextField.getText().equals("")) {
			selectInfoLabel.setText("请输入商品ID");
			return;
		}
		if (latelyPeriodComboBox.getSelectedIndex() == 1 && periodTextField.getText().equals("")) {
			selectInfoLabel.setText("请输入查询期数");
			return;
		}
		selectButton.setText("停止查询");
		selectInfoLabel.setText("正在查询...");
		goodsNameLblid.setText("");
		setButtonEnabled(false);
		TableModel tableModel = table.getModel();
		for (int i = 0; i < 15; i++) {
			for (int j = 0; j < 7; j++) {
				tableModel.setValueAt("", i, j);
			}
			if (verifyWinCheckBox.isSelected()) {
				tableModel.setValueAt("", i, 7);
			}
		}
		thread = new Thread(new Runnable() {
			public void run() {
				beginTime = System.currentTimeMillis();
				selectNum = 0;
				try {
					if (latelyPeriodComboBox.getSelectedIndex() == 0) {
						YunGouServer.setForeknow(goodsIdTextField.getText().trim(), null,
								Integer.toString(selectNumComboBox.getSelectedIndex() + 2), foreknowInterface);
					} else {
						YunGouServer.setForeknow(goodsIdTextField.getText().trim(), periodTextField.getText().trim(),
								null, foreknowInterface);
					}
				} catch (Exception e2) {
					selectButton.setText("查  询");
					selectInfoLabel.setText("该商品已下架或网络异常");
					setButtonEnabled(true);
				}
			}
		});
		thread.start();

	}

	/*
	 * 验证用户中奖
	 */
	private void verificationUserWinning(final DefaultTableModel tableModel, final String[] text, final int index2) {
		new Thread(new Runnable() {
			public void run() {
				try {
					if (YunGouLoginServer.getUserServer().getUserBuyDetail(text[7], text[2])) {
						tableModel.setValueAt("恭喜您中奖了", index2, 7);
					} else {
						tableModel.setValueAt("未中奖", index2, 7);
					}
				} catch (Exception e) {
					tableModel.setValueAt("验证出问题了", index2, 7);
				}
			}
		}).start();
	}

	private void setButtonEnabled(boolean isEnabled) {
		goodsIdTextField.setEnabled(isEnabled);
		periodTextField.setEnabled(isEnabled);
		selectNumComboBox.setEnabled(isEnabled);
		latelyPeriodComboBox.setEnabled(isEnabled);
		verifyWinCheckBox.setEnabled(isEnabled);
		if (isEnabled) {
			if (latelyPeriodComboBox.getSelectedIndex() == 0) {
				periodTextField.setEnabled(false);
			} else {
				selectNumComboBox.setEnabled(false);
			}
		}
	}

	public void setTextFieldContent(String goodsID, String period) {
		goodsIdTextField.setText(goodsID);
		periodTextField.setText(period);
	}

	public JFrameListeningInterface getJFrameListeningInterface() {
		if (selectButton.getText().equals("停止查询")) {
			return foreknowInterface;
		}
		return null;
	}

}
